@extends('common.base-master')

@section('html-head')
    <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
@endsection

@section('html-body')
    <div class="exconfirm-page">
        <div class="confirm-msg">
            <div class="product-count-box">
                <div class="product-img">
                    <img  src="@if(!is_null($order->good)){{route('Api.Image.ShowPX',['id'=>$order->good->thumb_id,'px'=>'300px'])}}@else {{url('images/default_product.png')}}@endif" alt="">
                </div>
                <div class="product-name">@if($order && $order->good){{$order->good->name}}@endif</div>
                <div class="product-price">单价：<span>@if($order && $order->good){{$order->good->amount}}@endif</span> 元</div>
                <div class="count-num">×@if($order){{$order->num}}@endif</div>
                <div class="total-price">
                    总价：<span>@if($order){{$order->amount}}@endif</span> 元
                </div>
            </div>
            <div class="border"></div>
            <div class="user-info">
                <div class="info-item">
                    姓名：@if($order){{$order->realname}}@endif
                </div>
                <div class="info-item">
                    手机：@if($order){{$order->phone}}@endif
                </div>
                <div class="info-item">
                    QQ：@if($order){{$order->qq}}@endif
                </div>
                <div class="info-item">
                    微信：@if($order){{$order->wechat}}@endif
                </div>
                <div class="info-item">
                    地址：@if($order){{$order->address}}@endif
                </div>
                <div class="info-item">
                    手机型号：@if($order){{$order->attr_name}}@endif
                </div>
                <div class="confirm-btn" id="btn_pay" data-id="@if($order){{$order->id}}@endif">微信支付</div>
            </div>
        </div>
        <div class="alert-container hidden">
            <div class="alert-box">
                <span class="bg-tick_b_icon"></span>
                <span class="alert-msg">商品购买成功</span>
                <span class="alert-tips">我们将在7个工作日内为您寄出商品请注意查收~</span>
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function () {

            $('.confirm-btn').click(function () {
                var id = $(this).data('id');
                startJsApiPay(id);
                {{--$.ajax({--}}
                    {{--url:"{{route('Api.Good.confirmOrder')}}",--}}
                    {{--type:'post',--}}
                    {{--dataType:'json',--}}
                    {{--data:{--}}
                        {{--order_id:id--}}
                    {{--},--}}
                    {{--success:function (obj) {--}}
                        {{--if(obj.level == 'success'){--}}
                            {{--$('.alert-container').removeClass('hidden');--}}
                            {{--setTimeout(function(){--}}
                                {{--window.location.href="{{route('Home.User.myexchange')}}?type=dfh";--}}
                            {{--}, 3000);--}}
                        {{--}else {--}}
                            {{--$.toast(obj.message,"forbidden");--}}
                        {{--}--}}
                    {{--},--}}
                    {{--error:function (obj) {--}}
                        {{--console.log(obj);--}}
                    {{--}--}}
                {{--});--}}
            });

            function startJsApiPay(order_num) {
                $.ajax({
                    type:"post",
                    url:'{{route('Api.Payment.payByWechatJsApi')}}',
                    dataType:"json",
                    data:{
                        order_num:order_num
                    },
                    success:function(obj){
                        if(obj.level == "success"){
                            $.hideLoading();
                            WXPayment(obj.data);
                        }else {
                            $.toast(obj.message, "forbidden");
                        }
                    },
                    error:function(obj){
                        console.log(obj);
                    }
                });
            }

            function WXPayment(configJSON) {
                if (typeof WeixinJSBridge === 'undefined') {
                    $.toast('请在微信在打开页面', "forbidden");
                    return false;
                }
                WeixinJSBridge.invoke('getBrandWCPayRequest', configJSON, function (res) {
                    switch (res.err_msg) {
                        case 'get_brand_wcpay_request:cancel':
                            alert('用户取消支付！');
                            break;
                        case 'get_brand_wcpay_request:fail':
                            alert('支付失败！（' + res.err_desc + '）');
                            break;
                        case 'get_brand_wcpay_request:ok':
                            $.toast('支付成功',function () {
                                window.location.href="{{route('Home.User.myexchange')}}?type=dfh";
                            });
                            break;
                        default:
                            alert(JSON.stringify(res));
                            break;
                    }
                });
            }
        })
    </script>
@endsection